package com.redlion.object;

import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.opengl.texture.region.ITextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.redlion.Game;

public class Shot extends AbstractObject {

	private static final float MULTIPLIER = 1;

	public Shot(Vector2 position, ITextureRegion mTextureRegion, FixtureDef mFixtureDef) {
		super(position, mTextureRegion, mFixtureDef);
	}

	public static Vector2 calcDir(Vector2 start, Vector2 end) {
		return (start.cpy().add(end)).nor();
	}

	public void shoot(Vector2 dir) {
		body.applyLinearImpulse(dir.mul(MULTIPLIER), body.getMassData().center);
	}

	@Override
	protected void createSprite(float px, float py) {
		sprite = new Sprite(px - (mTextureRegion.getWidth() / 2), py - (mTextureRegion.getHeight() / 2), mTextureRegion,
				Game.gameActivity.getVertexBufferObjectManager());
	}

	@Override
	protected void createSprite() {
		sprite = new Sprite(0, 0, mTextureRegion, Game.gameActivity.getVertexBufferObjectManager());
	}

	@Override
	protected void createBody(FixtureDef fixtureDef) {
		body = PhysicsFactory.createCircleBody(Game.mPhysicsWorld, sprite, BodyType.DynamicBody, fixtureDef);
		body.setBullet(true);
	}

	public void activate() {
		sprite.setIgnoreUpdate(false);
		sprite.setVisible(true);
		body.setAwake(true);
		body.setActive(true);
		body.setUserData(getClass());
	}

	public void deactivate() {
		sprite.setIgnoreUpdate(true);
		sprite.setVisible(false);
		body.setAwake(false);
		body.setActive(false);
		body.setUserData(null);
	}
}
